qui(
    function (require, exports, module) {
        VOD.NewsEdit = function (options) {
            this.dao = VOD.Dao;
            this.task = VOD.Task;
            this.config = VOD.Config;
            this.options = options || {};

        };
        Com.inherits(VOD.NewsEdit, VOD.Edit, function (options) {
        }).extend({
            newsInit: function () {
                var self = this;
                self.initNewsEdit();
                self.initTabs();
                self.init();
                self.initLoadImage();
                self.initAlterTitleDlg();
                self.initTab3();
            },
            initNewsEdit: function () {
                $("a[action-name=save-album-smart-library]").hide();
                $("a[action-name=save-ad-smart-library]").hide();
                $("#ocr").hide();
                $("#acr").hide();
                $("#inputStartTime").attr("disabled", "disabled");
                $("#inputEndTime").attr("disabled", "disabled");
            },
            //override
            initPointTime: function () {
                var self = this;
                var paramsData = self.dao.getParamsData();
                if (paramsData.startTime == null || paramsData.endTime == null) {
                    console.log("error");
                    return;
                }
                var s_d = paramsData.startTime;
                var e_d = paramsData.endTime;
                $('#inputStartTime').val(s_d);
                $('#inputEndTime').val(e_d);
                if (s_d >= e_d) {
                    $("#submitStartTime").prop("disabled", "disabled");
                    VOD.Plugin.tips("error", '开始时间必须小于结束时间');
                    return
                }
                VOD.Util.bindDate("inputStartTime", "inputEndTime", { maxDate: e_d, maxDateTime: new Date(e_d.replace(/-/g, "/")), minDate: s_d, minDateTime: new Date(s_d.replace(/-/g, "/"))});
                $("input[name=inputStartTime]").datetimepicker("setDate", new Date(s_d.replace(/-/g, "/")));
                $("input[name=inputEndTime]").datetimepicker("setDate", new Date(e_d.replace(/-/g, "/")));
                $("#submitStartTime").prop("disabled", "disabled");
                VOD.Config.INIT_SUCCESS = true;
            },
            initHelp2: function () {
                $("#help2-dlg").dialog({
                    title: "发布系统帮助",
                    resizable: false,
                    dialogClass: 'alert',
                    autoOpen: false,
                    width: 600,
                    modal: true
                })
                var innerText = "";
                $.each(VOD.Config.HELP2_DATA, function (i, data) {
                    innerText += '<tr><td>' + data[0] + '</td><td>' + data[1] + '</td></tr>'
                })
                $("#help2-dlg").find("tbody").html(innerText);
            },


            initTabs: function () {
                var self = this;
                $("#navDiv").tabs({active: 1});
                $("li[aria-controls=tab3]").hide();
                $("button[name=add_tab2]").hide();
                $("#navDiv").delegate("span.ui-icon-close", "click", function () {
                    $("li[aria-controls=tab3]").hide();
                    $("#tab3").hide();
                    $("#navDiv").tabs({active: 1});
                });


                $("a[href^=#tab]").bind("click", function (event) {
                    switch (event.currentTarget.id) {
                        case "ui-id-1":
                            console.log("show tab1");
                            VOD.AccurateFrame.doReset();
                            VOD.Util.clearHighLight();
                            //   self.cleanTab3();
                            break;
                        case "ui-id-2":
                            console.log("show tab2");
                           // VOD.Util.readGridSelectedFrameData();
                            $(".layout").click();
                            break;
                        case "ui-id-3":
                            $(".layout").click();
                            VOD.Util.readPublishFrameData();
                            console.log("show tab3");
                            break;
                    }
                })
            },
            initGrid: function () {
                var self = this;
                self.config.PageBeanFrame = VOD.Util.createPageBean();
                self.config.PageBeanAdvert = VOD.Util.createPageBean();
                self.config.PageBeanNews = VOD.Util.createPageBean();
                self.initFrameGrid();
                self.initAdvertGrid();
                self.initNewsGrid();
            },
            initNewsGrid: function (options, callback) {
                var self = this;
                var gridConfig = self.getGridConfig(self.config.TABLE_NEWS_LIST);
                options = $.extend(true, {}, gridConfig, options);
                self.options.$NewsGrid.jqGrid(options);
                $("#news_pager_left").css({width: "10px"});
                var htmlArr = [];
                htmlArr.push("<button type='button' name='refresh_tab2'>刷新</button>");
                htmlArr.push("<button type='button' onclick='VOD.Util.doQuickAdd()'>添加</button>");
                htmlArr.push("<button type='button' onclick='VOD.Util.doQuickMerge()'>合并</button>");
                htmlArr.push("<button type='button' onclick='VOD.Util.doSelectedAll(true)'>全选</button>");
                htmlArr.push("<button type='button' onclick='VOD.Util.doSelectedAll(false)'>清除选中</button>");
                $(htmlArr.join("")).appendTo($("#t_news_list"));
                $("button[name=refresh_tab2]").click(function (event) {
                    $("#navDiv").tabs({active: 1});
                    $("li[aria-controls=tab3]").hide();
                    $("#tab3").hide();
                    VOD.Util.clearHighLight();
                    self.queryNews();
                })

                //doAddTempRow
                self.queryNews();
            },
            queryNews: function () {
                var self = this;
                var taskid = VOD.Dao.settings.taskid;
                if (taskid == 0) {
                    console.log("queryNews taskid = 0");
                    return;
                }
                $("#news_list").jqGrid("setGridParam", {selrow: null});
                VOD.Util.clearHighLight();
                self.dao.getNewsList({key: taskid, from_client: true, index: 0, type: 1, offset: 100 }, function (data) {
                    if (data != null && data.list != null) {
                        var formatArr = [];
                        self.dao.settings.reg_last_id = 0;
                        var regId = self.dao.settings.reg_last_id;
                        var accurateFrameArr = [];
                        $.each(data.list, function (i, element) {
                            regId++;
                            accurateFrameArr.push(element.video_list[element.video_list.length - 1]);
                            formatArr.push({ title: element.text, sTime: element.video_list[0], eTime: element.video_list[element.video_list.length - 1], pic: element.cover, id: element.id, regId: regId,checked:false })
                        })
                        VOD.AccurateFrame.saveAccurateFrameArr(accurateFrameArr);
                        self.dao.settings.reg_last_id = regId
                        var lastSortSource = self.config.PageBeanNews.getSource();
                        if (lastSortSource.length > 0) {
                            formatArr = VOD.Util.doSourceMerge( lastSortSource ,formatArr);
                        }
                        self.config.PageBeanNews.setNewData(formatArr, 100);
                        VOD.Util.updateInfoComplete($("#inputEndTime").val(), data.list.length);
                    } else {
                        VOD.Util.updateInfoComplete($("#inputEndTime").val(), 0);
                        self.config.PageBeanNews.setNewData([], 100);
                    }
                    var retValue = self.config.PageBeanNews.getCur();
                    self.options.$NewsGrid.clearGridData();
                    self.options.$NewsGrid[0].addJSONData(retValue);

                });
            },

            initInterValTask: function () {
                var self = this;
                setInterval(self.task.getData, 5000);   // 获取图片数据

                //setInterval(getCate,20000);
                //      setInterval(self.task.getSty2, 19000);   // 智能识别数据
                VOD.Config.Global.sty2Data.list = [];
                //     setInterval(self.task.getOcr, 23000);
                //    setInterval(self.task.getAcr, 11000);
            },

            initAlterTitleDlg: function () {
                $("#alter_title-dlg").dialog({
                    title: "请选择该新闻标题",
                    resizable: false,
                    dialogClass: 'alert',
                    autoOpen: false,
                    width: 350,
                    //   minHeight:250,
                    modal: true
                })
                $("button[name=alter_title-ok]").bind("click", function (e) {
                    if (VOD.Util.isTab3Show()) {
                        VOD.Util.updateNewsFormData();
                    }
                    VOD.Util.updateNewsGridRowData();
                    $("#alter_title-dlg").dialog("close");
                });
                $("button[name=alter_title-cancel]").bind("click", function (e) {
                    $("#alter_title-dlg").dialog("close");
                });
            },
            initLoadImage: function () {
                if (VOD.Config.INIT_SUCCESS) {
                    var queryStringObj = VOD.Util.getUrlParams();
                    if (VOD.Plugin.Validate.checkDateFormat(queryStringObj["startTime"]) && VOD.Plugin.Validate.checkDateFormat(queryStringObj["endTime"])) {
                        $("input[name=inputStartTime]").datetimepicker("setDate", new Date(queryStringObj["startTime"].replace(/-/g, "/")));
                        $("input[name=inputEndTime]").datetimepicker("setDate", new Date(queryStringObj["endTime"].replace(/-/g, "/")));
                        $('#submitStartTime').click();
                    }
                }
            },
            initTab3: function () {
                var self = this;
                self.initSuggest2();
                $("button[name=tab3_title_select]").click(function (event) {
                    VOD.Util.AlterTitle(parseInt($("span[name=tabName]").html().replace(/序号|发布/g, "")));
                })
                $("button[name=tab3_review]").bind("click", function (event) {
                    var sTime = $("input[name=tab3_inpoint]").val();
                    var eTime = $("input[name=tab3_outpoint]").val();
                    VOD.Util.doReview(sTime, eTime, false)
                })
                VOD.Util.limitWordCount($("input[name=tab3_title]"), 43, null, null, function (length) {
                    $("span.leftWordsCount2").text(length);
                });
                $("button[name=tab3_submit]").bind("click", function (event) {
                    VOD.Util.news_submit();
                })
                $("input[name=tab3_outpoint]").bind("input propertychange", function (event) {
                    VOD.Util.updateTab3Check({ value: $("input[name=tab3_outpoint]").val(), key: "et"})
                })
                $("input[name=tab3_inpoint]").bind("input propertychange", function (event) {
                    VOD.Util.updateTab3Check({ value: $("input[name=tab3_inpoint]").val(), key: "st"})
                })
                $("input[name=tab3_imageId]").bind("input propertychange", function (event) {
                    VOD.Util.updateTab3Check({ value: $("input[name=tab3_imageId]").val(), key: "pic" })
                })
            },
            initSuggest2: function () {
                var self = this;
                var channelTypeSuggest = {
                    options: {
                        source: VOD.urlConfig.redirect,
                        renderPanel: function (handler) {
                            self.dao.getChannelType(handler)
                        },
                        select: function (event, object) {
                            console.log("initSuggest2  channelTypeSuggest");
                            $("input[name=tab3_albumName]").removeAttr("disabled");
                            $("input[name=tab3_albumName]").val("");
                            VOD.Util.setCooperativePartner_tab3(object.item.value);
                            return true;
                        },
                        autoFilter: false,
                        method: "post",
                        multiple: false,
                        parseValue: function () {
                            var val = this.element.val(),
                                ret = []
                            return ret;
                        },
                        getParam: function (term) {
                            var params = term.length == 0 ? {size: 100} : {word: term, size: 100}
                            var requestData = {url: VOD.Constant.thirdUrl_channel, type: "get", params: params, needCookie: true};
                            return {"request": JSON.stringify(requestData), timeSign: new Date().getTime()}
                        },

                        process: function (json) {
                            var ret = [],
                                self = this,
                                options = self.options,
                                itemData = null;
                            var data = null;
                            if (json == null) {
                                return [];
                            }
                            if (json.code === "A00000") {
                                data = $.isArray(json) ? json : json.data;
                            }
                            if (!data) {
                                return [];
                            }
                            $.map(data, function (item) {
                                itemData = {};
                                itemData[options.valueKey] = item["id"];
                                itemData[options.labelKey] = item["name"];
                                itemData["value"] = item["name"];
                                ret.push(itemData);
                            });
                            return ret;
                        },

                        template: function (item) {
                            return item["label"];
                        }
                    }
                }
                var albumSuggest = {
                    options: {
                        source: VOD.urlConfig.redirect,
                        method: "post",
                        multiple: false,
                        parseValue: function () {
                            return [];
                        },
                        getParam: function (term) {
                            var channelId = $("input[name=tab3_categoryName]").suggest("getValue")
                            var requestData = {url: VOD.Constant.thirdUrl_album, type: "get", params: {word: term, type: "displayName", filter_EQL_channel: channelId}, needCookie: true};
                            return {"request": JSON.stringify(requestData), timeSign: new Date().getTime()}
                        },
                        process: function (json) {
                            var ret = [],
                                self = this,
                                options = self.options,
                                itemData = null;
                            var data = null;
                            if (json == null) {
                                return [];
                            }
                            if (json.code === "A00000") {
                                data = $.isArray(json) ? json : json.data;
                            }
                            if (!data) {
                                return [];
                            }
                            $.map(data, function (item) {
                                itemData = {};
                                itemData[options.valueKey] = item["entityId"];
                                itemData[options.labelKey] = item["displayName"];
                                itemData["value"] = item["displayName"];
                                ret.push(itemData);
                            });
                            return ret;
                        },
                        template: function (item) {
                            return item["label"];
                        }
                    }
                }
                $("input[name=tab3_categoryName]").suggest(channelTypeSuggest.options);
                $("input[name=tab3_albumName]").suggest(albumSuggest.options);
            }

        })
    }
);